package pl.edu.utp.calculations;

import de.congrace.exp4j.Calculable;
import de.congrace.exp4j.ExpressionBuilder;
import de.congrace.exp4j.UnknownFunctionException;
import de.congrace.exp4j.UnparsableExpressionException;
import pl.edu.utp.ga.interfaces.Calculation;

public class RateFunction implements Calculation{
	
	private String equation;
	private String[] variables;
	private Calculable calculation;
	
	public RateFunction(String equation, String... variables) throws UnknownFunctionException, UnparsableExpressionException{
		this.equation = equation;
		this.variables = variables;
		createCalculation();
	}
	
	private void createCalculation() throws UnknownFunctionException, UnparsableExpressionException{
		calculation = new ExpressionBuilder(equation).withVariableNames(variables).build();
	}
	
	public String getEquation() {
		return equation;
	}

	public void setEquation(String equation) throws UnknownFunctionException, UnparsableExpressionException {
		this.equation = equation;
		createCalculation();
	}

	public String[] getVariables() {
		return variables;
	}

	public void setVariables(String... variables) throws UnknownFunctionException, UnparsableExpressionException {
		this.variables = variables;
		createCalculation();
	}

	@Override
	public double calculate(double[] values) {
		return calculation.calculate(values);
	}
}
